package com.itrip.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itrip.common.BigDecimalUtil;
import com.itrip.common.Constants;
import com.itrip.common.EmptyUtils;
import com.itrip.common.Page;
import com.itrip.entity.ItripHotelOrder;
import com.itrip.dao.ItripHotelOrderMapper;
import com.itrip.entity.ItripOrderLinkUser;
import com.itrip.entity.ItripUserLinkUser;
import com.itrip.service.ItripHotelOrderService;
import com.itrip.service.ItripHotelRoomService;
import com.itrip.service.ItripOrderLinkUserService;
import com.itrip.vo.order.ItripListHotelOrderVO;
import com.itrip.vo.order.ItripPersonalOrderRoomVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import static java.math.BigDecimal.ROUND_DOWN;

@Service
public class ItripHotelOrderServiceImpl extends ServiceImpl<ItripHotelOrderMapper, ItripHotelOrder> implements ItripHotelOrderService{

 private static final Logger LOGGER = LoggerFactory.getLogger(ItripHotelOrderServiceImpl.class);

 @Resource
 private ItripHotelRoomService itripHotelRoomService;
 @Resource
 private ItripOrderLinkUserService itripOrderLinkUserService;
 @Resource
 private ItripHotelOrderService itripHotelOrderService;
 @Resource
 private ItripHotelOrderMapper itripHotelOrderMapper;
 @Override
 public BigDecimal getOrderPayAmount(int count, Long roomId) throws Exception {
  BigDecimal payAmount = null;
  BigDecimal roomPrice = BigDecimal.valueOf(itripHotelRoomService.getById(roomId).getRoomPrice());
  payAmount = BigDecimalUtil.OperationASMD(count, roomPrice,
          BigDecimalUtil.BigDecimalOprations.multiply,
          2, ROUND_DOWN);
  return payAmount;
 }

 @Override
 public Map<String, String> itriptxAddItripHotelOrder(ItripHotelOrder itripHotelOrder, List<ItripUserLinkUser> itripOrderLinkUserList) throws Exception {
  Map<String, String> map = new HashMap<String, String>();
  if (null != itripHotelOrder) {
   Boolean flag;
   if (EmptyUtils.isNotEmpty(itripHotelOrder.getId())) {
    //删除联系人
    itripOrderLinkUserService.removeById(itripHotelOrder.getId());
    itripHotelOrder.setModifyDate(new Date());
    flag=itripHotelOrderService.updateById(itripHotelOrder);
   } else {
    itripHotelOrder.setCreationDate(new Date());
    flag=itripHotelOrderService.save(itripHotelOrder);
   }
   if (flag == true) {
    Long orderId = itripHotelOrder.getId();
    //添加订单之后还需要往订单与常用联系人关联表中添加记录
    if (orderId > 0) {
     for (ItripUserLinkUser itripUserLinkUser : itripOrderLinkUserList) {
      ItripOrderLinkUser itripOrderLinkUser = new ItripOrderLinkUser();
      itripOrderLinkUser.setOrderId(orderId);
      itripOrderLinkUser.setLinkUserId(itripUserLinkUser.getId());
      itripOrderLinkUser.setLinkUserName(itripUserLinkUser.getLinkUserName());
      itripOrderLinkUser.setCreationDate(new Date());
      itripOrderLinkUser.setCreatedBy(itripHotelOrder.getCreatedBy());
      itripOrderLinkUserService.save(itripOrderLinkUser);
     }
    }
    map.put("id", itripHotelOrder.getId().toString());
    map.put("orderNo", itripHotelOrder.getOrderNo());
    return map;
   }
  }
  return map;
 }

 @Override
 public ItripPersonalOrderRoomVO getItripHotelOrderRoomInfoById(Long orderId) throws Exception {
      return itripHotelOrderMapper.getItripHotelOrderRoomInfoById(orderId);
 }

 @Override
 public Page<ItripListHotelOrderVO> queryOrderPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
          Integer total = itripHotelOrderMapper.getOrderCountByMap(param);
          pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
          pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
          Page page = new Page(pageNo, pageSize, total);
          param.put("beginPos", page.getBeginPos());
          param.put("pageSize", page.getPageSize());
          List<ItripListHotelOrderVO> itripHotelOrderList = itripHotelOrderMapper.getOrderListByMap(param);
          page.setRows(itripHotelOrderList);
          return page;
 }
}
